package com.mjh.zhujianju.shiro;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.UserService;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTConstantUtils;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.model.set.PermissionEO;
import com.mjh.zhujianju.service.set.PermissionUserService;

/**
 * 用户身份验证,授权 Realm 组件
 * 
 **/
// @Component(value = "securityRealm")
public class SecurityRealm extends AuthorizingRealm {

	@Resource
	private UserService userService;

	@Resource
	private SysUserService sysUserService;

	@Resource
	private PermissionUserService permissionUserService;

	@Resource
	private FileService fileService;

	/**
	 * 权限检查
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		List<PermissionEO> list = permissionUserService.findPermissionByUserId(XTCurrentUser.getCurrentUserId());
		List<String> permissions = new ArrayList<>();
		if (!XTListUtils.isEmpty(list)) {
			for (PermissionEO p : list) {
				permissions.add(p.getMenuLink());
			}
			authorizationInfo.addStringPermissions(permissions);
		}
		return authorizationInfo;
	}

	/**
	 * 登录验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = String.valueOf(token.getPrincipal());
		String[] temp = username.split("===");
		username = temp[0];
		String type = null;
		if (temp.length > 1) {
			type = temp[1]; // 角色
		}
		String password = new String((char[]) token.getCredentials());
		SysUser user = null;
		try {
			user = this.userService.getAPILoginNameAndPassword(username, password, type);
			if (user == null || !user.getPassword().equals(password)) {
				throw new AuthenticationException("用户名或密码错误.");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new AuthenticationException(e.getMessage());
		}
		LoginUserInfo vo = new LoginUserInfo();
		BeanUtils.copyProperties(user, vo);
		vo.setPhone(user.getPhone());
		// 头像
		List<Long> files = fileService.getFileByCaseId(user.getId(), SysUser.class.getName(), null);
		if (!XTListUtils.isEmpty(files)) {
			vo.setHeadImage("/file/showImage/" + files.get(0));
		} else {
			vo.setHeadImage("/image/defualt.jpg");
		}
		SecurityUtils.getSubject().getSession().setAttribute(XTConstantUtils.LOGIN_USER_INFO, vo);
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
		return authenticationInfo;
	}
}
